﻿using System;
using System.IO;
using Microsoft.Xna.Framework;
using Terraria.Chat;
using Terraria.GameContent.UI.Chat;
using Terraria.Localization;
using Terraria.Net;
using Terraria.UI.Chat;

namespace Terraria.GameContent.NetModules
{
	// Token: 0x0200042C RID: 1068
	public class NetTextModule : NetModule
	{
		// Token: 0x06002506 RID: 9478 RVA: 0x00482490 File Offset: 0x00480690
		public static NetPacket SerializeClientMessage(ChatMessage message)
		{
			NetPacket result = NetModule.CreatePacket<NetTextModule>(message.GetMaxSerializedSize());
			message.Serialize(result.Writer);
			return result;
		}

		// Token: 0x06002507 RID: 9479 RVA: 0x0001A1A7 File Offset: 0x000183A7
		public static NetPacket SerializeServerMessage(NetworkText text, Color color)
		{
			return NetTextModule.SerializeServerMessage(text, color, byte.MaxValue);
		}

		// Token: 0x06002508 RID: 9480 RVA: 0x004824B8 File Offset: 0x004806B8
		public static NetPacket SerializeServerMessage(NetworkText text, Color color, byte authorId)
		{
			NetPacket result = NetModule.CreatePacket<NetTextModule>(1 + text.GetMaxSerializedSize() + 3);
			result.Writer.Write(authorId);
			text.Serialize(result.Writer);
			result.Writer.WriteRGB(color);
			return result;
		}

		// Token: 0x06002509 RID: 9481 RVA: 0x00482500 File Offset: 0x00480700
		private bool DeserializeAsClient(BinaryReader reader, int senderPlayerId)
		{
			byte b = reader.ReadByte();
			string text = NetworkText.Deserialize(reader).ToString();
			Color c = reader.ReadRGB();
			if (b < 255)
			{
				Main.player[(int)b].chatOverhead.NewMessage(text, Main.chatLength / 2);
				text = NameTagHandler.GenerateTag(Main.player[(int)b].name) + " " + text;
			}
			Main.NewTextMultiline(text, false, c, -1);
			return true;
		}

		// Token: 0x0600250A RID: 9482 RVA: 0x00482570 File Offset: 0x00480770
		private bool DeserializeAsServer(BinaryReader reader, int senderPlayerId)
		{
			ChatMessage message = ChatMessage.Deserialize(reader);
			ChatManager.Commands.ProcessReceivedMessage(message, senderPlayerId);
			return true;
		}

		// Token: 0x0600250B RID: 9483 RVA: 0x0001A1B5 File Offset: 0x000183B5
		private void BroadcastRawMessage(ChatMessage message, byte author, Color messageColor)
		{
			NetManager.Instance.Broadcast(NetTextModule.SerializeServerMessage(NetworkText.FromLiteral(message.Text), messageColor), -1);
		}

		// Token: 0x0600250C RID: 9484 RVA: 0x0001A1D3 File Offset: 0x000183D3
		public override bool Deserialize(BinaryReader reader, int senderPlayerId)
		{
			return this.DeserializeAsClient(reader, senderPlayerId);
		}
	}
}
